<template>
    <el-select v-model="infoName" :disabled="disabled||one" @change="change"  :multiple="isMultiple" placeholder="请选择"
               clearable>
        <el-option v-for="item in list" :key="item.code" :label="item.name" :value="item.code">
        </el-option>
    </el-select>
</template>

<script>
    import { getWLPort } from "../../api/port/port";

    export default {
        name: "selectPort",
        model: {
            prop: 'port',
            event: 'select'
        },
        props: {
            name: {
                default: ''
            },
            disabled: {
                type: Boolean,
                default: false
            },
            resNull: {
                type: Boolean,
                default: false
            },
            isMultiple: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                list: [],
                infoName: this.name
            }
        },
        created() {
            console.log(this.$store.getters.port)
            // const port = this.$store.getters.port
            const port = JSON.parse(sessionStorage.getItem('SET_PORT'))
            console.log(port)
            if (!port) {
                this.loadData()
            } else {
                this.list = port
                this.setChange()
            }
        },
        activated(){
            if(this.list.length>0){
                this.setChange()
            }
        },
        methods: {
            loadData() {
                getWLPort().then(res => {
                    this.$store.commit('SET_PORT', res.data.aaData)
                    sessionStorage.setItem('SET_PORT', JSON.stringify(res.data.aaData))
                    this.list = res.data.aaData
                    this.setChange()
                })
            },
            change(val) {
                this.$emit('select', val)
                console.log(val)
            },
            setChange() {
                if (this.resNull && this.list.length !== 1) {
                    this.$emit('select', '')
                    this.resNull = false
                } else {
                    if (this.list.length === 1) {
                        this.$emit('select', this.list[0].code)
                    }
                }
            }
        },
        computed: {
            one() {
                if (this.list.length === 1) {
                    if (this.isMultiple == true){
                        this.infoName = [this.list[0].code]
                        this.$emit('select', this.list[0].code)
                    } else {
                        this.infoName = this.list[0].code
                    }
                    // this.$emit('select', this.list[0].code)
                    return true
                } else {
                    return false
                }
            }
        },
        watch: {
            name(val) {
                this.infoName = val
                console.log(val)
            }
        }
    }
</script>

<style scoped>

</style>
